package com.rcloud.stock.ui.hq;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.rcloud.stock.R;
import com.rcloud.stock.adapter.TableRecyclerAdapter;
import com.rcloud.stock.util.BottomScrollView;

import org.w3c.dom.Text;

import java.util.Objects;

import static android.content.ContentValues.TAG;

public class HqFragment extends Fragment {

    //数据模型
    private HqViewModel hqViewModel;
    
    //静态常量
    private static final String[] sort_tabs = {
            "沪深A股","上证A股","深证A股","新股","中小板","创业板",
            "科创板","沪股通","深股通","B股","上证AB股比价",
            "深证AB股比价","风险警示板","两网及退市","港股","美股","英股"
    };
    private static final String[] en_metric_header = {
            "code","name","lp","zde","zdf","cjl","cje",
            "zf","high","low","hsl","syl","tmv","sjl","to",
            "zs","lb","time"
    };
    private static final String[] metric_header = {
            "代码", "名称", "最新价格", "涨跌额", "涨跌幅", "成交量", "成交额",
            "振幅", "最高", "最低", "换手率", "市盈率", "总市值", "市净率",
            "今开", "昨收", "量比", "时间"
    };
    private static final String[] value_body = {
            "600623","华谊集团","8.41","9.79","0.75","480741","393535424",
            "10.05","8.43","7.66","2.58","271.8","0.98","480741",
            "7.69","7.66","2.72","19921204"
    };
    //控件
    private TableLayout tableLayout;
    private TabLayout sortTabLayout;
    private RecyclerView tableRecyclerView;
    private EditText editText;
    private BottomScrollView verticalScroll;
    private LinearLayout verticalLinearLayout;

    //适配器
    private TableRecyclerAdapter tableRecyclerAdapter;
    private TableRow tableHeader;

    //方法
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        hqViewModel = new ViewModelProvider(this).get(HqViewModel.class);
        return inflater.inflate(R.layout.fragment_hq, container, false);
    }
    
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initAdapter();
        initView(view);
        initListeners();
        //异步初始化
        hqViewModel.fetchStocks(sort_tabs[0]);
    }

    private void initAdapter() {
        tableRecyclerAdapter = new TableRecyclerAdapter();
    }

    private void initListeners() {
        hqViewModel.getStocksLive().observe(getViewLifecycleOwner(),stocks -> {
            if (stocks == null) return;
            tableRecyclerAdapter.submitList(stocks);
        });

        //监听标签切换事件
        sortTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                hqViewModel.start = 0;
                hqViewModel.end = hqViewModel.pageSize;
                //请求数据
                hqViewModel.fetchStocks(tab.getText().toString());
                Log.d(TAG, "onTabSelected: ");
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) { }
            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });

        //监听editText聚焦事件
        editText.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus){
                NavController controller = Navigation.findNavController(v);
                controller.navigate(R.id.action_hqFragment_to_searchFragment);
            }
        });

        //滚动到底部加载
        verticalScroll.registerOnScrollViewScrollToBottom(new BottomScrollView.OnScrollBottomListener() {
            @Override
            public void scrollToBottom() {
                //加载数据
                hqViewModel.start = hqViewModel.end;
                hqViewModel.end += hqViewModel.pageSize;
                hqViewModel.fetchStocks(Objects.requireNonNull(Objects.requireNonNull(sortTabLayout.getTabAt(sortTabLayout.getSelectedTabPosition())).getText()).toString());
            }
        });
    }

    private void initView(View view) {
        //初始化顶部分类标签
        sortTabLayout = view.findViewById(R.id.stock_sort_tab);
        for (String sort : sort_tabs) {
            TabLayout.Tab tab = sortTabLayout.newTab();
            tab.setText(sort);
            sortTabLayout.addTab(tab);
        }
        //初始化表格头
        tableHeader = view.findViewById(R.id.stock_table_header);
        tableLayout = view.findViewById(R.id.stock_table);
        for (int i=0;i<18;i++){
            TextView textView = (TextView) tableHeader.getChildAt(i);
            textView.setText(metric_header[i]);
        }

        //初始化表格行
        tableRecyclerView = view.findViewById(R.id.stockRecyclerView);
        tableRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        tableRecyclerView.setAdapter(tableRecyclerAdapter);

        //初始化搜索栏
        editText = view.findViewById(R.id.searchEditText);

        //滚动
        verticalScroll = view.findViewById(R.id.verticalScrollView);
        verticalLinearLayout = view.findViewById(R.id.verticalLinearLayout);
    }
}